let app = getApp();
Page({
  "usingComponents": {},
  "data": {
    "phoneNumber": null,
    "verificationCode": null
  },

  "onPhoneInput": function (e) {
    this.setData({
      phoneNumber: e.detail.value
    });
  },

  "onCodeInput": function (e) {
    this.setData({
      verificationCode: e.detail.value
    });
  },

  // 用户登录
  "onLogin": async function () {
    const {
      phoneNumber,
      verificationCode
    } = this.data;
    const fixedVerificationCode = '0306'; // 固定验证码

    if (!phoneNumber) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      });
      return;
    }

    if (verificationCode !== fixedVerificationCode) {
      wx.showToast({
        title: '验证码错误，请重新输入',
        icon: 'none'
      });
      this.setData({
        verificationCode: ''
      }); // 清空验证码输入框
      return;
    }

    try {
      const res = await this.loginRequest(phoneNumber, verificationCode);

      // 检查服务器响应状态码
      if (res.statusCode !== 200) {
        wx.showToast({
          title: '服务器返回异常，请稍后再试',
          icon: 'none'
        });
        console.error("服务器返回异常:", res);
        return;
      }

      // 检查服务器响应数据
      if (res.data.code !== 200) {
        wx.showToast({
          title: res.data.message || '登录失败，请检查您的输入',
          icon: 'none'
        });
        return;
      }

      // 检查用户是否存在
      if (!res.data.data || !res.data.data.id) {
        wx.showToast({
          title: '用户不存在，请先注册',
          icon: 'none'
        });
        return;
      }

      // 登录成功
      var app = getApp();
      wx.showToast({
        title: '登录成功',
        icon: 'success'
      });
      app.globalData.isLogin = true;
      app.globalData.userID = res.data.data.id;
      console.log(app.globalData.userID);
      wx.setStorageSync('phoneNumber', phoneNumber);
      wx.switchTab({
        url: '/pages/my/my'
      });
      console.log(app.globalData.isLogin);
    } catch (error) {
      console.error("登录请求失败:", error);
      wx.showToast({
        title: '网络请求失败，请检查网络连接',
        icon: 'none'
      });
    }
  },

  // 用户注册
  "onRegister": function () {
    const phoneNumber = this.data.phoneNumber;
    const verificationCode = this.data.verificationCode;

    if (!phoneNumber) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      });
      return;
    }

    if (!verificationCode) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      });
      return;
    }

    return new Promise((resolve, reject) => {
      wx.request({
        url: 'http://localhost:8080/master/master',
        method: "POST",
        data: {
          mPhone: this.data.phoneNumber,
          verificationCode: this.data.verificationCode
        },
        success: resolve,
        fail: reject
      });
    }).then(res => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '注册成功',
          icon: 'success'
        });
        wx.setStorageSync('userID', res.data.data)
      } else {
        wx.showToast({
          title: res.data.message || '注册失败，请检查您的输入',
          icon: 'none'
        });
      }
    }).catch(error => {
      console.error("注册请求失败:", error);
      wx.showToast({
        title: '网络请求失败，请检查网络连接',
        icon: 'none'
      });
    });
  },

  "loginRequest": function (phone, code) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: 'http://localhost:8080/master/master/login',
        method: 'POST',
        data: {
          mPhone: phone,
          verificationCode: code
        },
        success: resolve,
        fail: reject
      });
    });
  }
});